package com.cos.lession57.web.controller;

import com.cos.lession57.util.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/3/11 9:27
 * @description:
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/download")
    public void downloadFile(String file, HttpServletResponse response,HttpSession session) throws IOException {
        System.out.println(file);
        String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
        //自己获取文件路径
//        String originFileName = file.split("_")[1];
        //如果用户文件名字有_       wahaha_xx.txt
        // asyyuunasubh23ui1h23123_wahaha_xx.txt
        //21
        String originFileName =file.substring(file.indexOf("_")+1);
        //1 设置响应类型 下载的响应 响应头
        //响应出去的时候,会进行二进制处理, a 中文字符串
        //URLEncoder.encode(originFileName,"utf-8") 把文件名进行编码,流量本身就是URLEncoder 就是url编码
        response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(originFileName,"utf-8"));
        //2 写出文件  响应文件出去
        IOUtils.copy(new FileInputStream(realPath+"\\"+file),response.getOutputStream());
    }

    @RequestMapping("/getDownloadList")
    public String getFileList(HttpSession session, Model model){
        //获取1,文件保存路径保存数据 数据库查询
        //获取2,upload/a/n/c/d 递归
        //1 获取文件保存的目录
        String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
        //2 创建file
        File f = new File(realPath);
        //3 创建map保存用户的文件路径和文件名
        //因为会有同名文件存在,所以key是路径 ,value 文件名
        Map<String,String> filePath = new HashMap<>();
        //3 获取文件目录下所有文件
        FileUtils.getFileList(f,filePath);
        //4 把获取到的数据保存到request作用域中
        model.addAttribute("fileList",filePath);
        return "download";
    }


    /**
     * @author: COS
     * @return:
     * @time: 2022/3/11 9:28
     * @description: spring 帮我们把上传的文件,封装为MultipartFile,方便我们进行处理
     */
    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpSession session) throws IOException {
        //文件名
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        //文件上传大小
        System.out.println(file.getSize());
        //获取的表单的name值
        System.out.println(file.getName());
        //动态获取 WEB-INF
        //session.getServletContext().getRealPath("") 获取项目部署的真实路径
        String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
        System.out.println("realPath:" + realPath);
        //创建file文件对象
        File f = new File(realPath);
        if (!f.exists()) {
            //创建目录使用
            f.mkdir();
        }
        //设置随机的文件名,防止覆盖的情况
        filename = FileUtils.renameFile(filename);
        //直接帮助我们完成文件复制
        file.transferTo(new File(realPath + "\\" + filename));
        return "index";
    }


}
